﻿using MobaCommon.Config;
using MobaCommon.Dto;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace MOBAServer.Room
{
    public class FightRoom : RoomBase<MobaClient>
    {
        #region 队伍1
        //英雄
        private Dictionary<int, HeroModel> team1HeroModel = new Dictionary<int, HeroModel>();
        //小兵
        private Dictionary<int, DogModel> team1DogModel = new Dictionary<int, DogModel>();
        //塔
        private Dictionary<int, TowerModel> team1TowerModel = new Dictionary<int, TowerModel>();

        #endregion

        #region 队伍2
        //英雄
        private Dictionary<int, HeroModel> team2HeroModel = new Dictionary<int, HeroModel>();
        //小兵
        private Dictionary<int, DogModel> team2DogModel = new Dictionary<int, DogModel>();
        //塔
        private Dictionary<int, TowerModel> team2TowerModel = new Dictionary<int, TowerModel>();
        #endregion

        #region Property
        /// <summary>
        /// 是否全部进入
        /// </summary>
        public bool IsAllEnter
        {
            get
            {
                return clientList.Count >= roomCount;
            }
        }
        public bool IsAllLeave
        {
            get 
            {
                return clientList.Count <= 0;
            }
        }
        /// <summary>
        /// 队伍1塔
        /// </summary>
        public TowerModel[] Towers {
            get {
                List<TowerModel> list = new List<TowerModel>();
                list.AddRange(team1TowerModel.Values);
                list.AddRange(team2TowerModel.Values);
                return list.ToArray();
            }
        }
        /// <summary>
        /// 队伍1英雄
        /// </summary>
        public HeroModel[] Heros
        {
            get
            {
                List<HeroModel> list = new List<HeroModel>();
                list.AddRange(team1HeroModel.Values);
                list.AddRange(team2HeroModel.Values);
                return list.ToArray();
            }
        }
        #endregion

        public List<MobaClient> leaveClient = new List<MobaClient>();
        public FightRoom(int id, int count) : base(id, count)
        {
        }
        /// <summary>
        /// 初始化房间
        /// </summary>
        /// <param name="team1"></param>
        /// <param name="team2"></param>
        public void Init(List<SelectModel> team1, List<SelectModel> team2)
        {
            //初始化英雄数据
            foreach (var item in team1)
            {
                team1HeroModel.Add(item.playerId, GetHeroModel(item,1));
            }
            foreach (var item in team2)
            {
                team2HeroModel.Add(item.playerId, GetHeroModel(item,2));
            }
            //初始化防御塔的数据
            team1TowerModel.Add(-10, GetTowerModel(-10,1,1));
            team1TowerModel.Add(-11, GetTowerModel(-11,2,1));
            team1TowerModel.Add(-12, GetTowerModel(-12,3,1));

            team2TowerModel.Add(-20, GetTowerModel(-20,1,1));
            team2TowerModel.Add(-21, GetTowerModel(-21,2,1));
            team2TowerModel.Add(-22, GetTowerModel(-22,3,1));
            
        }
        /// <summary>
        /// 根据英雄ID获取英雄数据
        /// </summary>
        /// <param name="heroId"></param>
        /// <returns></returns>
        private HeroModel GetHeroModel(SelectModel model,int team)
        {
            HeroDataModel data = HeroData.GetModel(model.heroId);
            //英雄数据创建
            HeroModel hero = new HeroModel(model.playerId,data.id,team,data.hp,data.baseAttack,data.baseDefens,data.attackDistance,data.heroName,data.mp,data.skillId);
            return hero;
        }
        /// <summary>
        /// 获取防御塔数据
        /// </summary>
        /// <param name="id">防御塔ID</param>
        /// <param name="typeId">防御塔类型</param>
        /// <param name="team">队伍</param>
        /// <returns></returns>
        private TowerModel GetTowerModel(int id,int typeId,int team)
        {
            //获取配置表里面的数据
            TowerDataModel data = TowerData.GetTowerData(typeId);
            TowerModel model = new TowerModel(data.Agressire,data.Rebirth,data.RebirthTime, id,typeId,team,data.Hp,data.Attack,data.Defense,data.AttackDistance,data.Name);

            return model;
        }
        private int dogId = -1000;
        public int DogId {
            get {
                dogId--;
                return dogId;
            }
        }
        /// <summary>
        /// 开启定时任务：30秒一波兵
        /// </summary>
        private void SpawnDog()
        {
            this.StartSchedule(DateTime.UtcNow.AddSeconds(30),
                delegate 
                {
                    List<DogModel> dogs = new List<DogModel>();
                    //产生小兵
                    for (int i = 0; i < 5; i++)
                    {
                        DogModel dog1 = new DogModel();
                        //TODO

                        //添加映射
                        team1DogModel.Add(dog1.Id, dog1);
                        dogs.Add(dog1);
                        DogModel dog2 = new DogModel();
                        //TODO

                        team2DogModel.Add(dog2.Id, dog2);
                        dogs.Add(dog2);
                    }
                    SpawnDog();
                });
        }
        /// <summary>
        /// 进入房间
        /// </summary>
        public void Enter(MobaClient client)
        {
            if(!clientList.Contains(client))
                clientList.Add(client);
        }

        public void Leave(MobaClient client)
        {
            if (clientList.Contains(client))
                clientList.Remove(client);

            if (!leaveClient.Contains(client))
                leaveClient.Add(client);
        }

        public void ClearData()
        {
            team1HeroModel.Clear();
            team1TowerModel.Clear();
            team1DogModel.Clear();
            team2HeroModel.Clear();
            team2TowerModel.Clear();
            team2DogModel.Clear();
            clientList.Clear();
            leaveClient.Clear();
        }
    }
}
